package core;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import client.TankClient;
import map.GMap;

/**
 * 游戏启动线核心器
 * @author lijia
 *
 */
public class StartLineCore {

	private BlockingQueue<TankClient> clients = null;
	private TankCore core = null;
	//房主
	//private TankClient host = null;
	//初始化
	public void init(TankCore core) {
		if(this.core == null) {
			this.core = core;
		}
		int number = core.getPlayerNumber();
		if(clients == null) {
			clients = new LinkedBlockingQueue<>(number);
		}
		//将数组清空
		if(!clients.isEmpty()) {
			clients.clear();
		}
	}
	
	public StartLineCore(TankCore core) {
		init(core);
	}
	
	public void join(TankClient client) {
		if(clients.remainingCapacity()<=0) {
			System.out.println("房间已满，不能再加入!");
			return;
		}
		
		clients.offer(client);
		ClientIdGenerator.getId(client);
//		if(clients.isEmpty()) {
//			this.host = client;
//		}
		if(clients.remainingCapacity()==0) {
			//通知房主可以开始游戏
			//this.core.startGameInform(this.host);
			//开始游戏
			this.core.startGame(this);
		}
	}
	
	public void start() {
		new GameController().start();
	}
	
	//游戏控制中心
	private class GameController extends Thread {

		//初始化游戏地图
		private GMap gmap = null;
		
		public GameController() {
		}
		
		@Override
		public void run() {
			System.out.println("游戏线程启动");
		}
	}
	
	//自定义的id生成器
	private static class ClientIdGenerator {
		
		private static int id = 0;
		
		public static void getId(TankClient client) {
			id++;
			client.setClientId("ROOM_"+String.format("%02d", id));
			System.out.println(client.getClientId()+" 玩家加入");
		}
		
	} 
	
}
